find_package(Lua)

set(UMQTT_LUA_SUPPORT_DEFAULT "ON")

if (NOT LUA_FOUND)
    set(BUILD_LUA_DEFAULT "OFF")
endif (NOT LUA_FOUND)

option(UMQTT_LUA_SUPPORT "Enable build of Lua module" ${UMQTT_LUA_SUPPORT_DEFAULT})

if(UMQTT_LUA_SUPPORT)
    add_library(umqtt-lua MODULE umqtt_lua.c)
    target_include_directories(umqtt-lua PRIVATE
        ${CMAKE_SOURCE_DIR}/src
        ${CMAKE_SOURCE_DIR}/src/buffer
        ${CMAKE_SOURCE_DIR}/src/log
        ${CMAKE_BINARY_DIR}/src
        ${LUA_INCLUDE_DIR})
    target_link_libraries(umqtt-lua PRIVATE umqtt)
    set_target_properties(umqtt-lua PROPERTIES OUTPUT_NAME umqtt PREFIX "")

    install(
        TARGETS umqtt-lua
        DESTINATION lib/lua/${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}
    )
endif(UMQTT_LUA_SUPPORT)
